﻿using BlondeHeaven.DAL.Interface;
using BlondeHeaven.DB;
using BlondeHeaven.DB.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BlondeHeaven.DAL
{
    public class DalShopKeeperService<T> : IDalShopKeeperService<T> where T : ShopKeeper, new()
    {
        /// <summary>
        /// 数据访问层：DAL
        /// </summary>
        /// <typeparam name="T"></typeparam>
        private BHDbContext _bHDbContext;
        /// <summary>
        /// 获得数据库上下文
        /// </summary>
        /// <param name="dbContext">数据库上下文类，各自更改成自己的</param>
        public DalShopKeeperService(BHDbContext bHDbContext)
        {
            _bHDbContext = bHDbContext;
        }

        public T Add(T model)
        {
            _bHDbContext.Set<T>().Add(model);
            return model;
        }
        public int SaveChanges()
        {
            return _bHDbContext.SaveChanges();
        }

        public IQueryable<T> GetAll()
        {
            return _bHDbContext.Set<T>().Where(m => m.IsRemo != false).AsNoTracking();
        }
        public T GetOneById(int Id)
        {
            var shop = GetAllShop().FirstOrDefault(m => m.Id == Id);
            return (T)shop;
        }
        public T EditAsync(T model)
        {
            _bHDbContext.Entry(model).State = EntityState.Modified;
            return model;
        }
        public T RemoveAsync(T model)
        {
            model.IsRemo = true;
            _bHDbContext.Entry(model).State = EntityState.Deleted;
            return model;
        }

        public List<ShopKeeper> GetAllShop()
        {
            return _bHDbContext.ShopKeepers.Where(m => m.IsRemo == false).ToList();
        }

        public List<ShopKeeper> GetOneByName(string Name)
        {
            return GetAllShop().Where(m => m.Name.Contains(Name)).ToList();
        }





    }
}
